package com.huli.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.Instant;


@Component
public class GateWayFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 记录请求的开始时间
        Instant start = Instant.now();
        // 执行请求并计算耗时
        return chain.filter(exchange).doOnTerminate(() -> {
            // 请求结束时，计算请求耗时
            Duration duration = Duration.between(start, Instant.now());
            System.out.println("------------------------------gateway调用服务消耗了" + duration.toMillis() + "ms---------------------");
        });
    }
}
